

package com.burger.counter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.burger.counter.Constants.SQLConstants;
import com.burger.counter.beans.Burger;
import com.burger.counter.util.DBConnection;

public class BurgerDao {
    public Burger[] getBurgerInBowl(int regionId){
        Connection con=null;
        ResultSet rs=null;
        PreparedStatement prest =null;
        Burger BurgerList[]=null;
        int size;
        try{
            con=DBConnection.getConnection();
            System.out.println("Creating DB Connection");
            String prepStatement=SQLConstants.getBurgerInBowl;
            prest = con.prepareStatement(prepStatement);
            prest.setInt(1,regionId);
            
            rs = prest.executeQuery();
            if (rs != null) {
                rs.last();
                size = rs.getRow();
                BurgerList = new Burger[size];
                int currRow = 0;
                rs.beforeFirst();

                while (rs.next()) {
                    Burger p = new Burger();
                    p.setBurgerId(rs.getInt(1));
                    p.setBurgerName(rs.getString(2));
                    p.setBurgerWeight(rs.getDouble(3));
                    p.setBurgerPrice(rs.getDouble(4));
                    p.setBurgerType(rs.getString(5));
                    BurgerList[currRow] = p;
                    currRow++;
                }
            }        

        }
        catch(Exception e){
            
        }
        
        return BurgerList;
    }


/*	public void setBurgerInBowlCount(int regionId,String burgerName, double burgerWeight,String burgerType){
		Connection con=null;
		int rs;
		PreparedStatement prest =null;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.updateBurgerCount;
			prest = con.prepareStatement(prepStatement);
			prest.setInt(1,regionId);
			prest.setString(2, burgerName);
			prest.setDouble(3, burgerWeight);
			prest.setString(4, burgerType);
			
			rs = prest.executeUpdate();
			if (rs ==1) {
				System.out.println("Burger Count has been updated");
			}					
		}
		catch(Exception e){
			
		}		
	}
   */ 
    public Burger[] getBurgerInBun(int regionId){
        Connection con=null;
        ResultSet rs=null;
        PreparedStatement prest =null;
        Burger BurgerList[]=null;
        int size;
        try{
            con=DBConnection.getConnection();
            System.out.println("Creating DB Connection");
            String prepStatement=SQLConstants.getBurgerInBun;
            prest = con.prepareStatement(prepStatement);
            prest.setInt(1,regionId);
            
            rs = prest.executeQuery();
            if (rs != null) {
                rs.last();
                size = rs.getRow();
                BurgerList = new Burger[size];
                int currRow = 0;
                rs.beforeFirst();

                while (rs.next()) {
                    Burger p = new Burger();
                    p.setBurgerId(rs.getInt(1));
                    p.setBurgerName(rs.getString(2));
                    p.setBurgerWeight(rs.getDouble(3));
                    p.setBurgerPrice(rs.getDouble(4));
                    p.setBurgerType(rs.getString(5));
                    BurgerList[currRow] = p;
                    currRow++;
                }
            }        

        }
        catch(Exception e){
            
        }
        
        return BurgerList;
    }

	public void setBurgerCount(int regionId,String burgerName, double burgerWeight, String burgerType){
		Connection con=null;
		int rs;
		PreparedStatement prest =null;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.updateBurgerCount;
			prest = con.prepareStatement(prepStatement);
			prest.setInt(1,regionId);
			prest.setString(2, burgerName);
			prest.setDouble(3, burgerWeight);
			prest.setString(4, burgerType);
			
			rs = prest.executeUpdate();
			if (rs ==1) {
				System.out.println("Burger Count has been updated");
			}					
		}
		catch(Exception e){
			
		}		
	}
    }
